package com.example.administrator.yuejian2.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.bean.LoginBean;
import com.example.administrator.yuejian2.model.UserModel;
public class LoginActivity extends BaseActivity {

    private Button login_button;
    private EditText username, password;
    private TextView forget_password, register;
    private RetrofitListener<LoginBean> listener;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initEvents();
        listener = new RetrofitListener<LoginBean>() {
            @Override
            public void onSuccess(LoginBean loginBean, int flag) {
                if (loginBean.getUser_id().equals("0")) {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                } else {
                    saveUid(loginBean);
                    sendSP();
                    Intent intent = new Intent(LoginActivity.this, FragmentActivity.class);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onFail() {
                Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
            }
        };

    }

    private void initView() {
        login_button = findViewById(R.id.login_button);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        forget_password = findViewById(R.id.forget_password);
        register = findViewById(R.id.register_login);
    }

    private void initEvents() {
        login_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });

        forget_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void login() {
        UserModel userModel = new UserModel();
        userModel.userLogin(username.getText().toString().trim(), password.getText().toString().trim(), listener);
    }

    private void sendSP() {
        sp = getSharedPreferences("user", Context.MODE_PRIVATE);
        String iphone = username.getText().toString();
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("iphone", iphone);
        editor.commit();
    }

    public void saveUid(LoginBean list) {
        String u_id = list.getUser_id();
        sp = getSharedPreferences("user_id", MODE_PRIVATE);//默认操作模式
        SharedPreferences.Editor editor = sp.edit();//通过edit()获取Editor对象
        editor.putString("userid", u_id);//通过editor对象存储键值对的操作
        editor.commit();//通过commit()提交数据
    }
}
